package com.bac.rentmap.web.controller.ad.transform;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.bac.rentmap.ad.flat.FlatAd;
import com.bac.rentmap.ad.transform.Result;
import com.bac.rentmap.ad.transform.CrawledAdTransformer;

/**
 * Transform {@link CrawledAdTransformer} to {@link FlatAd}.
 *
 * @author Andrew Osipenko
 */
public class AdTransformingController extends AbstractController {
    private CrawledAdTransformer<?> crawledAdTransformer;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        Result result = crawledAdTransformer.transform();
        response.getWriter().print(
                "Transformed " + result.getSourceItemCount() + " ads, created "
                        + result.getCreatedItemCount() + " new ads");
        return null;
    }

    public void setCrawledAdTransformer(
            CrawledAdTransformer<?> crawledAdTransformer) {
        this.crawledAdTransformer = crawledAdTransformer;
    }

}
